package com.ttbj.product.zwrefund.wxpay;
 
import java.util.SortedMap;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class ConfigUtil {
	
	private static Configuration configs;
	public static String APP_ID;	//服务号的应用ID
	public static String MCH_ID;	//商户号
	public static String API_KEY;	//API密钥
	
	public ConfigUtil() {
		init("wxinfo.properties");
	}
	
	public static synchronized void init(String filePath) {
		if (configs != null) {
			return;
		}
		try {
			configs = new PropertiesConfiguration(filePath);
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		if (configs == null) {
			throw new IllegalStateException("can`t find file by path:" + filePath);
		}
		APP_ID = configs.getString("APP_ID");
		MCH_ID = configs.getString("MCH_ID");
		API_KEY = configs.getString("API_KEY");
	}
	
	//微信和微信小程序支付统一退款接口地址
	public final static String REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
	//提交退款申请后，通过调用该接口查询退款状态
	public final static String REFUND_QUERY_URL = "https://api.mch.weixin.qq.com/pay/refundquery";
	
	//微信参数
	public static void wxCommomParams(SortedMap<Object, Object> packageParams) {
		//账号信息
		String appid = ConfigUtil.APP_ID;
		String mchid = ConfigUtil.MCH_ID;
		//生成随机字符串
		String currTime = PayCommonUtil.getCurrTime();
		String strTime = currTime.substring(8, currTime.length());
		String strRandom = PayCommonUtil.bulidRandom(4) + "";
		String nonce_str = strTime + strRandom;
		packageParams.put("appid", appid);	//公众账号id
		packageParams.put("mch_id", mchid);	//商户号
		packageParams.put("nonce_str", nonce_str);//随机字符串
	}
}
